package com.wolfalpha.utron.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormal2Activity;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.PartnerInfo;
import com.wolfalpha.utron.service.PartnerService;
import com.wolfalpha.utron.service.impl.PartnerServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;

/**
 * 个人基本信息编辑界面
 * <p/>
 * Created by luodon on 15/8/21.
 */
public class PartnerInfoSelfActivity extends BaseNormal2Activity {

    private RelativeLayout update1;  //升级为优秀合伙人的提示界面
    private LinearLayout update2;  //升级为认证合伙人的提示界面
    private LinearLayout authed;  //认证合伙人的信息界面

    private TextView notAuth;

    private ScrollView svDetail;    //详细信息

    private TextView tvRole;    //角色
    private TextView tvAbility;     //技能
    private TextView tvWilling;  //意愿
    private TextView tvTimein;    //时间投入
    private TextView tvExper;    //工作经验
    private TextView tvDy;    //代言

    private Button btnUpGood;  //升级为优秀合伙人
    private Button btnUpAuth;  //升级为认证合伙人

    private TextView tvYx;    //签名
    private TextView tvNj;    //签名
    private ImageView ivAuthPic;    //认证的照片

    private PartnerInfo partnerInfo;  //合伙人信息

    private PartnerService partnerService;

    private ProgressDialog pdLoad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partner_view_my);
        partnerService = new PartnerServiceImpl();

        initView();
        initListeners();
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        setTvTitle("我是合伙人");
        setRightIcon(R.mipmap.app_write);

        update1 = (RelativeLayout) findViewById(R.id.rl_partner_view_my_nothing1);
        update2 = (LinearLayout) findViewById(R.id.ll_partner_view_my_update2);

        svDetail = (ScrollView) findViewById(R.id.sv_partner_view_my_info);
        authed = (LinearLayout) findViewById(R.id.ll_partner_view_my_authed);

        notAuth = (TextView) findViewById(R.id.tv_partner_view_my_not_auth);

        btnUpGood = (Button) findViewById(R.id.btn_partner_view_my_update1);
        btnUpAuth = (Button) findViewById(R.id.btn_partner_view_my_update2);

        tvRole = (TextView) findViewById(R.id.tv_partner_view_my_role);
        tvAbility = (TextView) findViewById(R.id.tv_partner_view_my_ability);
        tvWilling = (TextView) findViewById(R.id.tv_partner_view_my_willing);
        tvTimein = (TextView) findViewById(R.id.tv_partner_view_my_timein);
        tvExper = (TextView) findViewById(R.id.tv_partner_view_my_experience);
        tvDy = (TextView) findViewById(R.id.tv_partner_view_my_dy);

        tvYx = (TextView) findViewById(R.id.tv_partner_view_my_auth_yx);
        tvNj = (TextView) findViewById(R.id.tv_partner_view_my_auth_nj);

        ivAuthPic = (ImageView) findViewById(R.id.iv_partner_edit_to_auth_photo);

        pdLoad = new ProgressDialog(this);
        pdLoad.setMessage("正在获取数据...");
    }

    private void initListeners() {
        btnUpGood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入优秀合伙人认证
                Bundle bundle = new Bundle();
                bundle.putBoolean("update", false);
                UIHelper.showActivity(PartnerInfoSelfActivity.this, PartnerToGoodActivity.class, bundle);
            }
        });

        btnUpAuth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入认证合伙人认证
                UIHelper.showActivity(PartnerInfoSelfActivity.this, PartnerToAuthActivity.class);
            }
        });
    }

    /**
     * 加载数据
     */
    private void loadData() {
        pdLoad.show();
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                partnerInfo = partnerService.getSelfInfo();
                if (partnerInfo == null) handler.sendEmptyMessage(FAIL);
                else handler.sendEmptyMessage(SUCCESS);
            }
        });
    }


    private final int SUCCESS = 1;
    private final int FAIL = 2;


    @Override
    public void rightOperation() {
        Bundle bundle = new Bundle();
        bundle.putBoolean("update", true);
        UIHelper.showActivity(PartnerInfoSelfActivity.this, PartnerToGoodActivity.class, bundle);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SUCCESS:
                    int l = partnerInfo.getLevel();
                    if (l == 1) {
                        update1.setVisibility(View.GONE);
                        svDetail.setVisibility(View.VISIBLE);
                        update2.setVisibility(View.VISIBLE);
                        authed.setVisibility(View.GONE);
                    } else if (l == 8) {
                        update1.setVisibility(View.GONE);
                        update2.setVisibility(View.GONE);
                        svDetail.setVisibility(View.VISIBLE);
                    } else if (l == 2) {
                        update1.setVisibility(View.GONE);
                        svDetail.setVisibility(View.VISIBLE);
                        update2.setVisibility(View.GONE);
                        authed.setVisibility(View.GONE);
                        notAuth.setText("正在审核");
                    }
                    if (l == 1 || l == 2 || l == 8) {
                        if (partnerInfo.getGood_partner() != null) {

                            tvRole.setText(partnerInfo.getGood_partner().getRole());
                            tvAbility.setText(partnerInfo.getGood_partner().getAbility());
                            tvWilling.setText(partnerInfo.getGood_partner().getWilling());
                            tvTimein.setText(partnerInfo.getGood_partner().getTimein());
                            tvExper.setText(partnerInfo.getGood_partner().getExperience());
                            tvDy.setText(partnerInfo.getGood_partner().getDy());
                        }
                    }
                    if (l == 8) {
                        if (partnerInfo.getAuth_partner() != null) {
                            notAuth.setVisibility(View.GONE);
                            tvYx.setText(partnerInfo.getAuth_partner().getYx() == null ? "" : partnerInfo.getAuth_partner().getYx());
                            tvNj.setText(partnerInfo.getAuth_partner().getNj() == null ? "" : partnerInfo.getAuth_partner().getNj());
                            Log.i("AA", partnerInfo.getAuth_partner().getAuth_pic());
                            Picasso.with(PartnerInfoSelfActivity.this)
                                    .load(partnerInfo.getAuth_partner().getAuth_pic())
                                    .placeholder(R.mipmap.head_default)
                                    .into(ivAuthPic);
                            ivAuthPic.setVisibility(View.VISIBLE);
                        }
                    }
                    break;
                case FAIL:
                    svDetail.setVisibility(View.GONE);
                    update1.setVisibility(View.VISIBLE);
                    break;
            }
            pdLoad.dismiss();
        }
    };


}
